﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main.Widget
{
    public partial class FrmInOperate : Form
    {
        public FrmExpDeal FrmCurExp { get; set; }

        public FrmInOperate()
        {
            InitializeComponent();
        }

        private void FrmInOperate_Load(object sender, EventArgs e)
        {

        }

        private void FrmInOperate_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void btnRestartTask_Click(object sender, EventArgs e)
        {
            if (ShowConfirmDialog("请确定", "重启任务将自动删除当前已入库数据，确定重启？") == DialogResult.OK)
            {
                WorkSpace.GetInstance().ReStartTask();
            }
        }

        private void btnFinishTask_Click(object sender, EventArgs e)
        {
            if (PublicObject.CurTask.state == (int)task_state.TS_RUN)
            {
                ShowInfoMessageBox("提示", "先暂停生产任务！");
                return;
            }
            else
            {
                if (ShowConfirmDialog("请确定", "结束任务将自动删除当前已入库数据，确定结束？") == DialogResult.OK)
                {
                    string strMsg = string.Empty;
                    WorkSpace.GetInstance().FinishTask(task_state.TS_STOP, ref strMsg);
                    ShowInfoMessageBox("提示", "结束任务！");
                    FrmMain.GetInstance().SwitchFormByModuleName("TaskRun");
                }

            }

        }

        private void btnReplaceCode_Click(object sender, EventArgs e)
        {
            FrmExpDeal frm = new FrmExpDeal();
            FrmCurExp = frm;

            frm.Tip = "码替换：先扫旧码，再扫新码";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                bool bRes = false;
                string strMsg = string.Empty;

                if (frm.Codes != null && frm.Codes.Count>=2)
                {
                    bRes = CodePrcUtility.ReplaceCode(frm.Codes, ref strMsg);

                    if (bRes)
                    {
                        ShowInfoMessageBox("提示", "替换成功！");
                    }
                    else
                    {
                        ShowInfoMessageBox("码替换失败", strMsg);
                    }
                }
                
            }
        }

        private void btnQueryCode_Click(object sender, EventArgs e)
        {
            FrmExpDeal frm = new FrmExpDeal();
            FrmCurExp = frm;
            frm.ShowCodeState = true;
            frm.IfCheckDuplicate = false;

            frm.Tip = "码查询：请扫描需要查询的条码";
            if (frm.ShowDialog() == DialogResult.OK)
            {//在FrmExpDeal窗体中已显示码信息
                //bool bRes = false;
                //string strMsg = string.Empty;

                //if (frm.Codes != null && frm.Codes.Count >= 1)
                //{
                //    bRes = CodePrcUtility.QueryCodeState(frm.Codes[0], ref strMsg);
                //    ShowInfoMessageBox("提示", strMsg);
                //}
            }
        }

        private void btnDeleteCode_Click(object sender, EventArgs e)
        {
            FrmExpDeal frm = new FrmExpDeal();
            FrmCurExp = frm;

            frm.Tip = "顺序删除：请顺序扫描需要删除的多个条码";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                bool bRes = false;
                string strMsg = string.Empty;

                if (frm.Codes != null && frm.Codes.Count >= 0)
                {
                    bRes = CodePrcUtility.DeleteInBatch(frm.Codes, ref strMsg);
                    WorkSpace.GetInstance().RefreshCodeQty();
                    ShowInfoMessageBox("提示", strMsg);
                }
            }
        }

        private void btnDeleteAdjacentCode_Click(object sender, EventArgs e)
        {
            FrmExpDeal frm = new FrmExpDeal();
            FrmCurExp = frm;
            frm.Ajacent = true;
            frm.Tip = "邻码删除：请扫描需要删除的条码（自动带出相邻条码）";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                bool bRes = false;
                string strMsg = string.Empty;

                if (frm.Codes != null && frm.Codes.Count >= 0)
                {
                    bRes = CodePrcUtility.DeleteInBatch(frm.Codes, ref strMsg);
                    WorkSpace.GetInstance().RefreshCodeQty();
                    ShowInfoMessageBox("提示", strMsg);
                }
            }
        }

        public DialogResult ShowConfirmDialog(string caption, string info)
        {
            return MessageBox.Show(this, info, caption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
        }

        public void ShowInfoMessageBox(string caption, string info)
        {
            MessageBox.Show(this, info, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}
